<%@page import="java.text.SimpleDateFormat"%>
<%@page import="java.text.Format"%>
<%@page import="java.util.Date"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="controller.Paginate"%>
<%@page import="com.mongodb.DBObject"%>
<%@page import="com.mongodb.DBCursor"%>
<%
DBCursor cursor = (DBCursor)request.getAttribute("cursor");
String remove = (String) request.getAttribute("remove");
String falses = (String) request.getAttribute("falses");
String path = (String) request.getAttribute("path");
int total = Integer.parseInt((String) request.getAttribute("total"));
int p = Integer.parseInt((String)request.getAttribute("page"));
int np = Integer.parseInt((String)request.getAttribute("np"));
int sumSource = Integer.parseInt((String)request.getAttribute("sumSource"));
int sum = Integer.parseInt((String)request.getAttribute("sum"));

DBObject users = (DBObject)session.getAttribute("users");
Format formatter = new SimpleDateFormat("MM-dd-yy");
%>

<jsp:include page="layout/header.jsp">
	<jsp:param name="title" value="Manager blog" />
	<jsp:param name="description" value="" />
	<jsp:param name="keywords" value="" />
	<jsp:param name="page" value="home" />
</jsp:include>

<div id="content">
        	<div id="content_inner">
            	<div id="maincontent">
                    <div id="domainmanager" class="clearafter">
                    	<div class="user-credit-info">
                    		<div class="left">                    			
                    			<p><span class="icon-money"></span><span class="number"><%=users.get("coin")!=null?users.get("coin"):0 %></span><span>Credit</span></p>
                    			<a href="/buy-credit" class="recent">Buy Credit</a>
                    		</div>
                    		<%
                    		if(users.get("coin")!=null)
                    		{
                    		%>
                    		<div class="center">
                    			<p class="iconColor-1">Total sources : <strong><%=sum %> sources</strong></p>
                    			<p class="iconColor-2">Total sources are activated : <strong><%=sumSource %> sources</strong></p>
	                    		<p class="iconColor-3">Total days for using : <strong><%=(sumSource!=0)?Integer.parseInt(users.get("coin").toString())/sumSource:"0" %> days</strong></p>
                    		</div>
                    		<%
                    		}
                    		%>	  
                    		<div class="rights">
                    			<p class="iconColor-4">Created : <strong><%=formatter.format(new Date(Long.parseLong(users.get("created").toString()))) %></strong></p>
                    			<%
                    			if(users.get("status").toString().equals("trial"))
                    			{
                    			%>
                    			<p class="iconColor-4">Expires : <strong><%=formatter.format(new Date(Long.parseLong(users.get("timeend").toString()))) %></strong></p>
	                    		<p class="iconColor-5">Type : <strong>Trial</strong></p>
	                    		<%
                    			}else{
	                    		%>
	                    		<p class="iconColor-5">Type : <strong>Pro</strong></p>
	                    		<%
                    			}
	                    		%>
                    		</div>                  	
	                    	<div class="clearbreak"></div>
                    	</div>
                        <h2 class="title">Manager Blogs<%=remove!=null?"<span style='color:#21759B;'> ( Remove blog "+remove+" success ) </span>":"" %>
                        <%=falses!=null?"<span style='color:red;'> ( Remove blog false ! ) </span>":"" %>
                        </h2>
                        <div class="domain-toolbar title">
                        	<a href="/create-blog" style="float: left;">Create new blogs</a>
                        	<form method="get" action="">
                            	<div>
                                    <label>
                                        Blog name : 
                                        <input type="text" name="name"/>
                                    </label>
                                </div>
                                <div>
                                    <label>
                                        Blog type : 
                                    </label>
                                    <select name="type">
                                        <option value="">All</option>
                                        <option value="wordpress">Wordpress</option>
                                        <option value="blogger">Blogger</option>
                                    </select>
                                </div>
                                <button type="submit">Search</button>
                            </form>
                            <div style="clear: both;"></div>
                        </div>
                        <div style="clear: both;"></div>
                        <%
                        if(cursor.size()>0)
                        {
                        %>
                        <form action="/renew" method="post">
	                        <ul>
	                        	<%
	                        	while(cursor.hasNext())
	                        	{
	                        		DBObject dbObject = cursor.next();
	                        	%>
	                        		<%
	                            			long time = Long.parseLong(dbObject.get("timeend").toString())-new Date().getTime();
	                            			time = time/(24*60*60*1000);
	                            			if(time<0)time=0;
	                            	%>
		                            <li class="tooltips">
		                            	<a href="/work?blog=<%=URLEncoder.encode(dbObject.get("blogurl").toString()) %>" class="icon-<%=dbObject.get("type") %>"><%=dbObject.get("blogurl") %></a></span>
		                                <div class="action">
		                                	<span><a href="/work?blog=<%=URLEncoder.encode(dbObject.get("blogurl").toString()) %>" class="icon-add">Add sitemap to the blog</a></span>
		                                    <span><a href="/remove?blog=<%=URLEncoder.encode(dbObject.get("blogurl").toString()) %>" class="icon-trash" onclick="javascript:if(!confirm('Do you want remove blog ?'))return false;">Remove</a></span>
		                                    <span><a href="/edit?blog=<%=URLEncoder.encode(dbObject.get("blogurl").toString()) %>" class="icon-screwdriver">Edit</a></span>
		                                </div>
		                            </li>
	                            <%
	                        	}
	                            %>
	                        </ul><br/>
                        </form>
                        <%
                        } else {
                        %>
                        Currently, you do not have any blogs. <a href="/create-blog" style="display: inline;">Click here</a>
                        <%
                        }
                        %>
                        
                    </div>
                </div>
                <%
                if(total>20)
                {
                %>
                <%=Paginate.paginate(p,np,7,path) %>
                <%
                }
                %>
            </div>
        </div>      
<%@ include file='/layout/footer.jsp'%>        